package com.ft.adbix.core.ad.manager;

import android.text.TextUtils;

import com.ft.adbix.core.listener.RewardListener;
import com.ft.adbix.core.response.Ad;

import java.util.HashMap;
import java.util.Map;

public class RewardAdManager {

    private Map<String, RewardListener> rewardListenerMap;
    private Map<String, Ad> rewardAdDataMap;

    private static final class InstanceHolder {
        static final RewardAdManager instance = new RewardAdManager();
    }

    public static RewardAdManager getInstance() {
        return InstanceHolder.instance;
    }

    private RewardAdManager() {
        rewardListenerMap = new HashMap<>();
        rewardAdDataMap = new HashMap<>();
    }

    public void saveRewardAdData(Ad ad) {
        if (rewardAdDataMap != null && ad != null) {
            rewardAdDataMap.put(ad.getReqId(), ad);
        }
    }

    public void saveRewardAdListener(String reqId, RewardListener listener){
        if (rewardListenerMap != null && listener != null && !TextUtils.isEmpty(reqId)) {
            rewardListenerMap.put(reqId, listener);
        }
    }

    public void removeReward(String reqId){
        if (rewardListenerMap != null && !TextUtils.isEmpty(reqId)) {
            rewardListenerMap.remove(reqId);
        }
        if (rewardAdDataMap != null && !TextUtils.isEmpty(reqId)) {
            rewardAdDataMap.remove(reqId);
        }
    }

    public RewardListener getRewardListener(String reqId) {
        try {
            if (rewardListenerMap != null && !TextUtils.isEmpty(reqId)) {
                return rewardListenerMap.get(reqId);
            }
        } catch (Exception ignored) {

        }
        return null;
    }


    public Ad getRewardAdData(String reqId) {
        try {
            if (rewardAdDataMap != null && !TextUtils.isEmpty(reqId)) {
                return rewardAdDataMap.get(reqId);
            }
        } catch (Exception ignored) {

        }
        return null;
    }

}
